//
//  MyFamilyController.m
//  WheatPlan
//
//  Created by Billy on 15/7/23.
//  Copyright (c) 2015年 IOSTeam. All rights reserved.
//

#import "MyFamilyController.h"
#import "WebAccessUtil.h"
#import "Baby.h"
#import "CommonUtil.h"

@interface MyFamilyController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *array_currentPageDataSource; //数据源

@end

@implementation MyFamilyController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self pageSettingsAndSubviewsCreation];
}


#pragma mark - TableView datasource && delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    return [self.array_currentPageDataSource count];
    if (self.array_currentPageDataSource) {
        
    }
    return  5;
}



- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifer = @"cell" ;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];
    }
    
    // construct cell
    
    [self createSubViewForCell:cell atIndexPath:indexPath];
    
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0:
            return (76*3+22)/2;
            break;
        
            
        case 3:
            return  11.0/2 +93/2;
            break;
        default:
            return 93/2;
            break;
    }
    
}

#pragma mark - SubviewsCreationAndPageSettings

- (void) pageSettingsAndSubviewsCreation {
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"我家"];
    
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]   initWithTitle:@"宝宝"  style:UIBarButtonItemStylePlain target:self
                                                                  action:@selector(BabySelectionPage)];
    
    [rightBtn setTintColor:[UIColor whiteColor]];
    [self.navigationItem setRightBarButtonItem: rightBtn];
    
    
    // tableView creation
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    
    [self.view addSubview:self.tableView];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

// 创建子cell
- (void) createSubViewForCell:(UITableViewCell *) cell atIndexPath:(NSIndexPath *) indexPath {
    
    switch (indexPath.row) {
        case 0: //宝宝信息显示栏
        {
            
            UIImageView *imageView_backGroundView  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (76*3+22)/2)];
            
            [imageView_backGroundView setImage: [UIImage imageNamed:@"_0001_圆角矩形-2"]];
            [cell.contentView addSubview:imageView_backGroundView];
            
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(30, 98/2, SCREEN_WIDTH-60, 18)];
         
            
            label.text =[NSString stringWithFormat:@"您的宝宝%@已经%@岁了",  [[NSUserDefaults standardUserDefaults] objectForKey:@"babyName"],  [[NSUserDefaults standardUserDefaults] objectForKey:@"babyAge"]];
            
            [label setFont :[UIFont systemFontOfSize:18] ];
            [label setTextAlignment :NSTextAlignmentCenter];
            [label setAdjustsFontSizeToFitWidth:YES];
            
            [imageView_backGroundView addSubview:label];
            
            //身高
            UILabel * lab1 = [[UILabel alloc]init];
            lab1.frame = CGRectMake(SCREEN_WIDTH/2-140, (98+76)/2, 140, 11);
            lab1.text = [NSString stringWithFormat:@"身高：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"babyHeight"]];
            lab1.font = [UIFont systemFontOfSize:11];
            lab1.textAlignment = NSTextAlignmentLeft;
            [imageView_backGroundView addSubview:lab1];
            
            //体重
            UILabel * lab2 = [[UILabel alloc]init];
            lab2.frame = CGRectMake(SCREEN_WIDTH/2+20, (98+76)/2, 140, 11);
            lab2.text =  [NSString stringWithFormat:@"体重：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"babyWeight"]];
            lab2.font = [UIFont systemFontOfSize:11];
            lab2.textAlignment = NSTextAlignmentLeft;
            [imageView_backGroundView addSubview:lab2];
            
        }
            
            break;
            
        case 1:
        {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 10, 22, 22)];
            
            [imageView setImage:[UIImage imageNamed:@"1"]];
            [cell.contentView addSubview:imageView];
            
        }
            break;
        case 2:
        {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 10, 22, 22)];
            
            [imageView setImage:[UIImage imageNamed:@"2"]];
            [cell.contentView addSubview:imageView];
        }
            break;
            case 3:
        {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 10, 22, 22)];
            
            [imageView setImage:[UIImage imageNamed:@"3"]];
            [cell.contentView addSubview:imageView];
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 93.0/2, SCREEN_HEIGHT, 11.0/2)];
            [view setBackgroundColor:[UIColor lightGrayColor]];
            [cell.contentView addSubview:view];
        }
            break;
            
            case 4:
        {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 10, 22, 22)];
            
            [imageView setImage:[UIImage imageNamed:@"5"]];
            [cell.contentView addSubview:imageView];

        }
            break;
            
            break;
        default:

            break;
    }
    
}



#pragma mark - Button click events handling

- (void) BabySelectionPage {
    
    
}


#pragma mark - Data preparation 

- (NSArray *) array_currentPageDataSource {
    
    if (!_array_currentPageDataSource) {
        
        NSString *url = [NSString stringWithFormat:@"%@%@",kNetPath_Code_Base,kNetPath_Code_homeCollection];
        NSString *ID = [[NSUserDefaults standardUserDefaults] objectForKey:@"ID"];
        NSDictionary *params = @{@"userId": ID};
        
        [[WebAccessUtil shardWebUtil] accessMyFamilyUsingURLString:url andParas:params andTransferGottenDataBack:^(id obj) {
            
            _array_currentPageDataSource = obj;
            
            if(obj){ //获取了数据
                [MBProgressHUD hideHUDForView:self.view animated:YES]; //隐藏
                
                //指定默认宝宝
                
                Baby *tmp = _array_currentPageDataSource[0];
                [CommonUtil setDefaultBaby:tmp];
                
                [self.tableView reloadData];
            }else {
                [MBProgressHUD hideHUDForView:self.view animated:YES]; //隐藏
                ////NSLog(@"no data gotten!");
            }
        }];
        
    }
    
    return _array_currentPageDataSource;
}




@end
